<?php

class Authlogin {

     var
             $CI;
     var
             $user_table = 'adm_user';
     var
             $field_id = 'username_adm';
     var
             $field_username = 'username_adm';
     var
             $field_password = 'password';
     var
             $admin_path = 'admin';
     var
             $nama_cookie = 'cookie_admin';

     /*
       --------------------------------------------------------------------------------
      */

     function Authlogin()
     {
          $this->CI = & get_instance();
          $cokie = $this->CI->input->cookie($this->nama_cookie);
          if ($cokie != null)
          {
               $cokie = unserialize($this->CI->input->cookie($this->nama_cookie));
               $this->CI->session->set_userdata($cokie);
               $this->CI->session->set_userdata(array('logged_in' => true));
          }
     }

     /*
       --------------------------------------------------------------------------------
      */

     function isLoggedin($user)
     {
          return ($this->CI->session->userdata($this->field_id) == $user && $this->CI->session->userdata('logged_in'));
     }

     /*
       --------------------------------------------------------------------------------
      */

     function chkLoggedin($user)
     {
          if (!$this->CI->authlogin->isLoggedin($user))
          {
               $this->logout();
               $this->CI->session->set_flashdata('Forced Logout');
               redirect($this->admin_path);
          };
     }

     /*
       --------------------------------------------------------------------------------
      */

     function login($user = '', $password = '', $remember = false, $user_data = 'logged_in')
     {
          if ($user == '' OR $password == '')
          {
               return false;
          }
          if ($this->isLoggedin($user))
          {
               $this->logout();
               return false;
          };
          $this->CI->db->where($this->field_username, $user);
          $query = $this->CI->db->get_where($this->user_table);
          //echo $this->CI->db->last_query();
          if ($query->num_rows() > 0)
          {
               $row = $query->row_array();
               if (md5($password) == md5($row[$this->field_password]))
               {
//                    $this->CI->session->sess_destroy();
//                    $this->CI->session->sess_create();
                    unset($row[$this->field_password]);
                    $this->CI->session->set_userdata($row);
                    
                    $this->CI->session->set_userdata(array($user_data => true));
                    if ($remember == true)
                    {
                         $serialized = serialize($row);
                         $this->simpan_cookie($serialized);
                    }
                    return true;
               }
               else
               {
                    return false;
               }
          }
          else
          {
               return false;
          }
     }

     function login_user($user = '', $password = '', $remember = false, $user_data = 'logged_in')
     {
          if ($user == '' OR $password == '')
          {
               return false;
          }
          if ($this->isLoggedin($user))
          {
               $this->logout();
               return false;
          };
          $this->CI->db->where('username', $user);
          $query = $this->CI->db->get_where('user_login');
          //echo $this->CI->db->last_query();
          if ($query->num_rows() > 0)
          {
               $row = $query->row_array();
               if (md5($password) == md5($row['password']))
               {
//                    $this->CI->session->sess_destroy();
//                    $this->CI->session->sess_create();
                    unset($row[$this->password]);
                    $this->CI->session->set_userdata($row);
                    
                    $this->CI->session->set_userdata(array($user_data => true));
                    if ($remember == true)
                    {
                         $serialized = serialize($row);
                         $this->simpan_cookie($serialized);
                    }
                    return true;
               }
               else
               {
                    return false;
               }
          }
          else
          {
               return false;
          }
     }

     /*
       --------------------------------------------------------------------------------
      */

     function logout($id_session = '')
     {
         if($id_session == '')
         {
          $this->CI->session->sess_destroy();
          delete_cookie($this->nama_cookie);
         }
         else
         {
             $this->CI->session->unset_userdata(array(
                 $id_session => FALSE,
                 ));
         }
          $this->CI->load->helper('cookie');
     }

     function simpan_cookie($value)
     {
          $cookie = array(
              'name' => $this->nama_cookie,
              'value' => $value,
              'expire' => '259200',
              'path' => '/'
          );
          $this->CI->input->set_cookie($cookie);
     }

}

?>